Insem Paper 2024 - Complete Solution

Step-by-step solutions for all questions with diagrams and flowcharts.

Q.1

Q.1 (a) Draw and explain flowchart of pass I of two pass assembler with example.

Pass 1 Assembler Flowchart

Pass 1 Assembler Flowchart

Explanation:

Pass 1 of a two-pass assembler scans the source program to determine the addresses of all instructions and data definitions. It does not generate the final object code, but instead builds important tables such as the Symbol Table, Literal Table, and Pool Table, while producing intermediate code for use in Pass 2.

Step 1: Initialize

Read START statement (if present) → set LC = starting address.
Initialize SYMTAB, LITTAB, POOLTAB as empty.


Step 2: Read Source Line by Line

  • If label exists → Add to SYMTAB with LC value.
  • If opcode is a machine instruction → Lookup size in MOT → Increment LC.
  • If opcode is declarative (DC, DS) → Allocate storage → Update LC.
  • If operand contains a literal → Add to LITTAB (address assigned later at LTORG/END).
  • If opcode is LTORG or END → Assign addresses to pending literals → Update POOLTAB.

Step 3: Generate Intermediate Code

For each line, record LC, opcode class/type, symbol/literal references, and flags for Pass 2.


Step 4: End of Pass 1

Final SYMTAB, LITTAB, POOLTAB completed; Intermediate code ready for Pass 2.


Pass 1 of the assembler lays the groundwork for actual code generation in Pass 2. By scanning the program once and maintaining all necessary address and symbol information, it ensures that forward references are resolved and that machine code can be generated systematically in the second pass.

Q.1 (b) Differentiate :
i. Literal and Immediate operand.
ii. Assembler and Compiler

Literal Operand

A literal is a constant value explicitly written in the program but stored in a literal pool (not directly inside the instruction).

The assembler creates an entry for each literal in the Literal Table (LITTAB), assigns it a memory location, and during execution the instruction uses the address of that literal.

Example:

MOVER AREG, ='5'

='5' is a literal.

The assembler stores 5 in memory (literal pool).

The instruction moves the value from that memory location into the register.

Immediate Operand

An immediate operand is a constant value directly embedded in the instruction itself, not stored in memory separately.

There is no LITTAB entry for immediate operands because the value is part of the instruction.

Example:

MOVEM AREG, 5

5 is an immediate operand.

The instruction encodes 5 directly inside its machine code.

No separate memory is needed to store it.


Comparision between Immediate Operand and Literal


Parameter Immediate Operand Literal
Instruction Size & Speed Immediate operand is fast. Value is part of the instruction, no memory lookup needed. Literal allows reusing the same value at multiple places without repeating in instructions.
Memory Optimization Good for one-time use (small constants). Good when same constant is used multiple times — stored once in a literal pool.
Assembler Handling No assembler table needed. LITTAB allows assemblers to manage constants systematically.
Example MVI AREG, 5
Move Immediate: Uses immediate addressing mode.
MOVER AREG, =5
ADD AREG, =5

Comparision between Assembler and Compiler


Parameter Assembler Compiler
Definition An assembler is a system software that translates an assembly language program into its equivalent machine language program (object code). A compiler is a system software that translates a high-level language program into its equivalent machine language program or intermediate code.
Input Language Takes assembly language instructions, which are low-level mnemonics specific to a particular processor. Takes high-level language programs, which are written in human-readable statements such as loops, conditionals, and functions.
Output Produces machine code or object code that is directly executable by the target processor. Produces machine code or sometimes intermediate code, which may then be assembled or interpreted into executable code.
Portability The output program is not portable because assembly language is processor-dependent. The output program is more portable because the same high-level source code can be compiled on different platforms with different compilers.
Passes/Phases Usually implemented as a one-pass or two-pass assembler. Consists of multiple phases: lexical analysis, syntax analysis, semantic analysis, optimization, and code generation.
Complexity Relatively simple because it directly converts mnemonics to machine opcodes and resolves addresses. More complex because it analyzes the program structure, checks semantics, and performs optimization before generating code.
Execution Speed of Output The generated machine code runs very fast because it is close to hardware instructions. The generated machine code also runs fast, and in some cases faster than assembly code if the compiler applies advanced optimizations.

Q.2

Q.2 (a) Consider following Assembly code and show output of pass-I of two pass Assembler with entries in Mnemonic Opcode Table, Pseudo Opcode Table, Symbol Table, Literal Table and Pool Table. [8]
PROG START 50
USING PROG+2, 15
L1, FIVE
Al, = F ‘2’
LTORG
ST 1, RES
FIVE DC F ‘4’

RES DS F ‘4’

RES DS IF

END

red highlighted text denotes printing mistake in question paper

corrected code is given below


PROG START 50
USING PROG+2, 15
L 1, FIVE
A l, = F ‘2’
LTORG
ST 1, RES
FIVE DC F ‘4’
RES DS 4F
END

Answer:

Step 1: Processing of Location Counter

Line Statement LC
1 PROG START 50 --
2 USING PROG+2, 15 --
3 L1 L 1, FIVE 50
4 A 1, =F'2' 54
5 LTORG
=F'2'
58
6 ST 1, RES 62
7 FIVE DC F'4' 66
8 RES DS 4F 70
9 END --

In IBM 360 / similar assemblers (John J. Donovan examples): The basic data unit is a word. One word = 4 bytes. Most instructions and fullword constants occupy one word (4 bytes) in memory. Hence the LC will be incremented by 4.

Symbol Table

Index Symbol Address Remark (optional)
0 PROG 50 START doesn’t consume LC, but label on START line takes LC value set by START.
That’s why PROG = 50 in SYMTAB.
1 L1 50
2 FIVE 66
3 RES 70

Literal Table

Index Literal Address
0 =F'2' 58

Pool Table

Pool Table Pointer Start Index in LITTAB
0 0

Pseudo Opcode Table

Pseudo Op Meaning
START Specifies starting address for LC
USING Specifies base register and address
LTORG Assigns addresses to literals in pool
DC Defines constant storage
DS Reserves storage
END Indicates end of program

Machine Opcode Table

Sr. No. Opcode Class Code Length
1 L IS 04 4
2 A IS 02 4
3 ST IS 05 4
4 DC DL 01 -
5 DS DL 02 -
6 START AD 01 -
7 USING AD 02 -
8 LTORG AD 05 -
9 END AD 06 -

Intermediate Code

Sr. No. Source Code Line LC Intermediate Code
1 PROG START 50 -- (AD,01) (C,50)
2 USING PROG+2, 15 -- (AD,02) (C, 52)
3 L1 L 1, FIVE 50 (IS,04) (RG,01) (S,2)
4 A 1, =F'2' 54 (IS,02) (RG,01) (L,0)
5 LTORG
=F'2'
--
58

(DL,02) (C,2)
6 ST 1, RES 62 (IS,05) 1 (S,3)
7 FIVE DC F'4' 66 (DL,01) (C,4)
8 RES DS 4F 70 (DL,02) (C,4)
9 END -- (AD,06)

After END the value of LC will be 86. as LC will be incremented One word = 4 bytes. Here for RES 4F size is given so 4*4bytes=16. The previous value of LC is 70. So 70 +16 =86

Q.2 (b) Enlist and explain the necessity of different data structures used in Pass-I of two pass Assembler?

In Pass-I of a two-pass assembler, several data structures are essential to prepare all the information required by Pass-II for final machine code generation.

List of data structures:

  • Symbol Table (SYMTAB)
  • Machine Opcode Table (MOT)
  • Literal Table (LITTAB)
  • POOL Table (POOLTAB)
  • Base Table (Specific for certain machines like IBM 360)
  • Pseudo Opcode Table (Specific for certain machines like IBM 360)
  • Intermediate Code
  • Location Counter (LC)

Symbol Table (SYMTAB)

The Symbol Table stores all symbols, such as labels and variable names, along with their addresses. As Pass-I scans the program, it encounters these symbols and records their addresses in SYMTAB. This ensures that in Pass-II, symbolic names can be directly replaced by their corresponding numeric addresses.

Literal Table (LITTAB)

The Literal Table stores all literals that appear in the program. Whenever a literal such as =‘5’ is encountered, it is added to LITTAB. The actual addresses of these literals are assigned when the assembler processes a literal pool, which occurs at the LTORG directive or at the END of the program.

Literal Pool Table (POOLTAB)

The POOLTAB is necessary to manage multiple literal pools in a program. It records the starting index of each literal pool in LITTAB. This allows the assembler to correctly locate and assign addresses to literals in different sections of the program, especially when LTORG is used multiple times.

Intermediate Code

The Intermediate Code is essential because it stores an intermediate representation of the program after Pass-I. This intermediate code contains information such as operation codes, register numbers, and indices to symbols or literals, which are resolved in Pass-II.

Location Counter (LC)

The Location Counter is required to keep track of the memory address currently being assigned to instructions, symbols, and literals. It is updated sequentially as each statement is processed in Pass-I, ensuring correct address allocation throughout the program.

Base Table

The Base Table is used in certain machine architectures, such as the IBM 360, for base-relative addressing. It stores base register information and displacement limits so that addresses beyond direct reach can be resolved.

Q.3

Q.3 (a) Explain the Phases of Compiler and their output with an example.

Intermediate Code Example

Q.3 (b) Define macro. What are the advantages of macro facility? How they are different from function.

Definition of Macro

A macro is a named block of instructions or statements, optionally with parameters.

In assembly, it is defined using MACRO–MEND, and in high-level languages with preprocessor directives.

At each call, its code is expanded in place during assembly or preprocessing, before compilation or execution.

During macro expansion, the assembler replaces the macro call with the actual sequence of instructions defined in the macro body.

A macro can accept parameters (arguments), which are substituted into the macro body during expansion, allowing code customization at different call sites.

Macros are processed by the macro processor (part of the assembler), and expansion happens before the assembly process, so the assembler works on the expanded code.

Macro Syntax (Assembly)



MACRO
MACRONAME &PARAM1, &PARAM2, ...
    ; Body of macro using parameters
MEND
    

Macro Example

MACRO
INCR &USE_REG1
    ADD &USE_REG1, ='1'
MEND

    INCR AREG   ; Macro call – expands into ADD AREG, ='1'
    INCR BREG   ; Macro call – expands into ADD BREG, ='1'
    

Expanded Code for Macro Call INCR AREG is:

    ADD AREG, ='1'
    

Expanded Code for Macro Call INCR BREG is:

    ADD BREG, ='1'
    

Advantages of Macro Facility

1. Code Reusability

A macro allows you to define a block of instructions once and use it in multiple places by simply calling its name. This avoids writing the same instructions repeatedly.

Benefit: Saves programming effort, reduces chances of errors, and makes programs shorter to write.

MACRO
INCR &USE_REG
    ADD &USE_REG, ='1'
MEND

    INCR AREG
    INCR BREG
    

2. No Function Call Overhead

In functions, control transfers to another block of code (function call) and then returns. This involves pushing parameters onto the stack, jumping to the function, and returning, which takes extra time. Macros, however, are expanded inline — there is no jump or return; the instructions are directly inserted.

Benefit: Faster execution since there is no overhead of function call and return.

#define SQUARE(x) ((x)*(x)) // Macro
int result = SQUARE(5);     // Expands to (5*5)
    

3. Parameterization

Macros can take parameters (arguments), allowing customization of the expanded code without redefining the macro.

Benefit: The same macro works for different registers, values, or operations.

MACRO
ADDVAL &USE_REG, &VAL
    ADD &USE_REG, &VAL
MEND

    ADDVAL AREG, X
    ADDVAL BREG, Y
    

4. Improved Readability

Long or repetitive instruction sequences can be hidden behind a meaningful macro name, making the main program easier to read and maintain.

Benefit: The code looks cleaner, more structured, and easier to understand for others.

ADD_TWO_NUMS NUM1, NUM2, RESULT
    

5. Speed of Execution

Since macros expand inline, there is no runtime lookup or call overhead. The CPU executes the expanded instructions directly.

Benefit: Execution speed is higher (but code size increases due to repeated expansion).

#define CUBE(x) ((x)*(x)*(x))
int c = CUBE(4); // Expands to (4*4*4)
    

Macro vs Function

Aspect Macro Function
Definition A macro is a block of code that is replaced by the preprocessor before the program is compiled. It is essentially a text substitution mechanism. A function is a separate block of instructions that is executed when it is called at runtime.
Execution A macro is expanded inline at every place it is invoked, meaning the macro code is directly inserted into the program at each call, avoiding control transfer. A function is executed by transferring control to its body during runtime and then returning control to the caller, which involves a small performance overhead.
Speed A macro executes faster because it avoids function call overhead; the expanded code runs directly as part of the program. A function is slightly slower due to the overhead of control transfer and return during function calls.
Memory Usage A macro increases program size because its code is expanded in multiple places wherever it is invoked. A function saves memory because its code is stored only once, and every call uses the same single copy.
Flexibility A macro cannot return values and cannot be recursive because it is not a runtime construct. A function can return values and can be recursive, allowing it to call itself as part of problem-solving.

Q.4

Q.4 (a)What is the use of AIF and AGO pseudo-op in macro? Explain macro expansion with positional parameter with the help of suitable example. [8]

AIF vs AGO in Macro Processing

AIF (Assembler IF)

Meaning:
AIF stands for Assembler IF. It is used inside a macro for conditional branching during macro expansion.

Purpose:
If a given condition is true, the macro processor jumps to the statement label specified. If the condition is false, expansion continues sequentially.

Syntax:

AIF (condition/expression) .<sequencing symbol>

Example:

AIF (&ARG1.EQ.&ARG2) .EQUAL

If ARG1 equals ARG2, control jumps to the label EQUAL.
If false, execution continues to the next line.


AGO (Assembler GO)

Meaning:
AGO stands for Assembler GO. It is used for unconditional branching during macro expansion.

Purpose:
It always transfers control to the specified label without checking any condition.

Syntax:

AGO .<sequencing symbol>

Example:

AGO .FINISH

Control always jumps to the label FINISH.


  • AIF is like a conditional IF statement for macros.
  • AGO is like an unconditional GOTO statement for macros.
  • Both operate at macro expansion time, not at runtime.

Macro Expansion with Positional Parameters

Definition of Positional Parameters

Positional parameters are placeholders used in a macro definition, which are replaced by actual arguments during macro expansion based on their position in the macro call.

They are usually represented as &ARG1, &ARG2 in the macro definition.

Macro Definition

A macro is defined once with positional parameters representing the values to be substituted during the call.

Macro Call

When the macro is called, actual arguments are passed in the same order as parameters.

Expansion

The macro processor replaces positional parameters with actual arguments at each call, inserting the expanded code into the program. The substitution is purely positional:

  • The first argument replaces &ARG1
  • The second argument replaces &ARG2

Example

Macro Definition:
MACRO
ADDNUM &ARG1, &ARG2
    MOVER AREG, &ARG1
    ADD  AREG, &ARG2
MEND

&ARG1 → First positional parameter (First number)
&ARG2 → Second positional parameter (Second number)

Macro Call:
ADDNUM NUM1, NUM2
Macro Expansion (After expansion):
MOVER AREG, NUM1
ADD  AREG, NUM2

The macro processor replaces &ARG1 with NUM1 and &ARG2 with NUM2 according to their positions. This avoids rewriting the same addition code for different variable pairs.

Q.4 (b) Explain the concept of single pass Macro processor with example. Give example for macro calls within the macro.

Definition

A single-pass macro processor is a type of macro processor that performs macro definition handling and macro expansion in the same single scan (pass) of the source program. The program is read only once, and macro calls are expanded immediately when encountered. There is no separate preprocessing or second pass to expand macros.

Immediate expansion: When a macro call is detected, its body is substituted right away into the program. The assembler then works on the expanded program directly.


Working of Single Pass Assembler

  1. Read Source Program Line by Line: The macro processor scans each line of the program sequentially.
  2. Handling Macro Definitions:
    • If a MACRO directive is encountered:
      • Store the macro name in MNT (Macro Name Table).
      • Store its body (between MACRO and MEND) in MDT (Macro Definition Table).
  3. Handling Macro Calls:
    • When a macro name is found in the program:
      • Search for it in the MNT.
      • Retrieve its body from the MDT.
      • Substitute arguments (if any) for positional parameters.
      • Insert the expanded code immediately into the output.
  4. Continue Processing: The macro processor keeps reading the next lines, expanding macros on the fly.

Data Structures Used

  • MNT (Macro Name Table): Stores each macro’s name and a pointer to where its definition starts in MDT.
  • MDT (Macro Definition Table): Stores the sequence of statements of each macro.
  • ALA (Argument List Array): Stores actual arguments during macro expansion for substitution.

Advantages

  • Faster – Only one pass over the program, resulting in quicker processing.
  • Less I/O – No second scan required, reducing disk/memory operations.
  • Direct Output – Expanded program is generated immediately.

Disadvantages

  • Nested macro calls (macro calling another macro) can be tricky since the inner macro might not be defined before the outer macro is expanded.
  • No forward referencing of macros – A macro must be defined before it is used, otherwise expansion fails.
  • Error handling is difficult – Since expansion happens immediately, errors in macro bodies may appear later in the program, making debugging harder.

Single-Pass Macro Processor Example

Macro Definition

MACRO
INCR &REG
    ADD &REG, ='1'
MEND

&REG is a positional parameter representing the register to be incremented.
This macro adds 1 to the register.

Program with Macro Calls

START 100
    MOVER AREG, NUM
    INCR AREG
    INCR BREG
NUM DC '5'
END

How Single-Pass Macro Processor Works

  1. Reads the first line: START 100 → Copies it to output.
  2. Next line: MOVER AREG, NUM → Copies it to output.
  3. Next line: INCR AREG → Macro call detected.
    • Finds INCR in MNT.
    • Fetches its body from MDT.
    • Substitutes &REG with AREG.
    • Expands immediately in output.
  4. Next line: INCR BREG → Macro call detected.
    • Substitutes &REG with BREG.
    • Expands immediately in output.
  5. Remaining lines are copied as they are.

Expanded Code (Output after Single Pass)

START 100
    MOVER AREG, NUM
    ADD AREG, ='1'
    ADD BREG, ='1'
NUM DC '5'
END

Why It Works in One Pass

  • The macro is defined before it is called.
  • As soon as the processor encounters a macro call, it expands it immediately without going back for another scan.
  • No second pass is required.
  • Expansion is immediate.
  • Works well when macros are defined before use.